﻿using System;
using System.IO;
using System.Text;
using System.Threading;

namespace LevelDB
{
    class Program
    {
        static void Main(string[] args)
        {
            TestLevel();
        }

        static void CreateLevel()
        {
            Level.Instance.SetLevelDirectory(string.Empty);
            Level.Instance.CreateLevel(new LevelInfo("hello"));

            Level.Instance.OnGameClose();
        }

        static void TestLevel()
        {
            Level.Instance.SetLevelDirectory(string.Empty);
            //Level.Instance.CreateLevel(new LevelInfo("hello"));
            Level.Instance.LoadLevel("hello");
            {
                var chunk = Level.Instance.GetChunkDataAsync(0, 0, 0);
                while (chunk.Waiting) continue;

                chunk.data[0, 0, 0] = 1;
                for (int i = 0; i < 16; i++)
                {
                    chunk.SetData((i, i, i), Encoding.UTF8.GetBytes("A")[0]);
                }
                chunk.active = false;
            }

            Level.Instance.RequireSaveChunks();
            Level.Instance.PrintLevelStatus();

            {
                var chunk = Level.Instance.GetChunkData(-1, 0, 0);

                Console.WriteLine("chunk:(-1, 0, 0) :1, 0, 0: " + chunk.data[1, 0, 0]);

                for (int i = 0; i < 16; i++)
                {
                    chunk.SetData((i, 0, 0), 2);
                    chunk.data[0, i, 0] = 3;
                    chunk.data[0, 0, i] = 4;
                }


                chunk.ReleaseRef();
            }

            
            Level.Instance.RequireSaveChunks();
            Console.WriteLine();
            Level.Instance.PrintLevelStatus();

            Console.WriteLine();
            Thread.Sleep(200);
            Level.Instance.PrintLevelStatus();

            Level.Instance.CloseLevel();
            Level.Instance.OnGameClose();
        }
    }
}
